
using QFramework;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Projectsurviva
{
	
	public partial class EnemyGenerator : ViewController
	{
		private float mcurrentsecond = 0f;
		private float mcurrentgenerateseconds = 0f;
		public static BindableProperty<int> enemynum = new BindableProperty<int>(0);

        [SerializeField]
		
		private Queue<EnemyWave> mQueue=new Queue<EnemyWave>();
		public LevelConfig config;
		private int wavenum;
		public int totalcount;
 		public bool lastwave=>wavenum== totalcount;
		void Start()
		{
            foreach (var item in config.enemywavegroups)
            {
                foreach (var item1 in item.Waves)
                {
					mQueue.Enqueue(item1);
					totalcount++;
                }
            }

          
        }
		private EnemyWave mcurrentwave = null;
		public EnemyWave Mcurrentwave => mcurrentwave;


        private void Update()
        {
			if(mcurrentwave==null)
			{
				if(mQueue.Count>0)
				{
					wavenum++;
					mcurrentwave=mQueue.Dequeue();
					mcurrentsecond = 0;
					mcurrentgenerateseconds = 0;

                }
			}
			if(mcurrentwave.EnemyPrefab!=null)
			{
				mcurrentsecond += Time.deltaTime;
				mcurrentgenerateseconds += Time.deltaTime;

				
                if (mcurrentsecond>=mcurrentwave.GenerateDuration)
				{
					mcurrentsecond = 0;
                    if (Play.Default != null)
                    {
						var xory = RandomUtility.Choose(-1, 1);
						var pos = Vector2.zero;

						if(xory==-1)
						{
							pos.x = RandomUtility.Choose(CameraContoller.Lbtrans.position.x, CameraContoller.Rttrans.position.x);
							pos.y = UnityEngine.Random.Range(CameraContoller.Lbtrans.position.y, CameraContoller.Rttrans.position.y);
						}
						else
						{
                            pos.x = UnityEngine.Random.Range(CameraContoller.Lbtrans.position.x, CameraContoller.Rttrans.position.x);

                            pos.y = RandomUtility.Choose(CameraContoller.Rttrans.position.y, CameraContoller.Rttrans.position.y);

						}
                        //var player = Play.Default.transform.position;
                        //var rndangle = UnityEngine.Random.Range(0, 360f);
                        //var rndradius = rndangle * Mathf.Deg2Rad;
                        //var dirction = new Vector3(Mathf.Cos(rndradius), Mathf.Sin(rndradius));
                        //var generatorpos = player + dirction * 10;
					
						
						
                        mcurrentwave.EnemyPrefab .Instantiate().Position(pos)

							.Self(self => 
							{
								 var enemy=self.GetComponent<IEnemy>();
								enemy.SetSpeedScale(mcurrentwave.speedScale);
								enemy.SetHpScale(mcurrentwave.hpScale);

							})
							.Show();
						}
                    
                }
                if (mcurrentgenerateseconds >= mcurrentwave.seconds)
                {
                    mcurrentwave = null;
                }

            }else
			{
				Debug.Log("nuill");

			}

        }
    }
}
